using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;
using Lion;
using Telerik.WinControls.UI;
namespace LionForms
{
    public partial class AccountsForms : Telerik.WinControls.UI.RadForm
    {
        private Lion.Account AccountUpdated;
        private Lion.AccountCategory AccountCategoryUpdated;
        private Lion.AccountClass AccountClassUpdated;

        public AccountsForms()
        {
            InitializeComponent();

            tabItemViewAllAccounts.IsSelected = true;
            gvAllAccounts.DataSource = Account.All.Values.ToList();

            ddlAccountTypes.DataSource = AccountCategory.All.Values.ToList();



        }

        private void tabItemAccountTypes_Click(object sender, EventArgs e)
        {
            gvAccountTypes.DataSource = Lion.AccountCategory.All.Values.ToList();
            ddlAccountClass.DataSource = Lion.AccountClass.All.Values.ToList();
            ddlAccountClass.DisplayMember = "Name";
        }

        private void btnCreateType_Click(object sender, EventArgs e)
        {
            string typeName = txtAccountType.Text;
            int typeID;
            Lion.AccountClass accountClass = (Lion.AccountClass)ddlAccountClass.SelectedValue;
            if (!int.TryParse(txtAccountTypeID.Text, out typeID))
            {
                RadMessageBoxExtensions.Show("Invalid ID (Number Required!)", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                return;
            }

            if (Lion.AccountCategory.All.ContainsKey(typeID))
            {

                RadMessageBoxExtensions.Show("ID already exists", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                return;
            }

            Exception ex;
            AccountCategory accountCategory = new AccountCategory() { ID = typeID, Name = typeName, AccountClass = accountClass };
            ObjectAssembler.SaveCachedObject<int, AccountCategory>(accountCategory, accountCategory.ID, accountCategory, out ex);

            if (null != ex)
            {
                RadMessageBoxExtensions.Show("Error While Saving Account Type", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);

            }
            else
            {
                RadMessageBoxExtensions.Show("Saved Successfuly");
                tabItemAccountTypes_Click(null, null);
            }


        }

        private void btnCreateAccount_Click(object sender, EventArgs e)
        {

            AccountCategory accountCategory = (AccountCategory)ddlAccountTypes.SelectedValue;

            string name = txtAccountName.Text;
            string id = txtAccountIDf.Text + "-" + txtAccountIDs.Text.Trim();

            if (Account.All.ContainsKey(id))
            {
                RadMessageBoxExtensions.Show("ID already exists", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                return;
            }

            Account account = new Account()
            {
                ID = id,
                Name = name,
                AccountCategory = accountCategory,
                IsActive = true
            };

            Exception ex;
            ObjectAssembler.SaveCachedObject<string, Account>(account, account.ID, account, out ex);

            if (null != ex)
            {
                RadMessageBoxExtensions.Show("Error While Saving new Account", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);

            }
            else
            {
                RadMessageBoxExtensions.Show("Saved Successfuly");
                txtAccountName.Text = string.Empty;
                txtAccountIDs.Text = string.Empty;
                txtAccountIDf.Text = string.Empty;
                tabItemViewAllAccounts_Click(null, null);
            }

        }

        private void ddlAccountTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            AccountCategory category = ddlAccountTypes.SelectedValue as AccountCategory;
            if (null != category)
            {
                AccountClass aclass = category.AccountClass;
                txtAccountIDf.Text = aclass.ID + "" + category.ID;

            }
        }

        private void tabItemViewAllAccounts_Click(object sender, EventArgs e)
        {
            gvAllAccounts.DataSource = Lion.Account.All.Values.ToList();

            ddlAccountTypes.DataSource = Lion.AccountCategory.All.Values.ToList();

        }

        private void tbAccountClass_Click(object sender, EventArgs e)
        {
            gvAccountClasses.DataSource = Lion.AccountClass.All.Values.ToList();
        }

        private void btnCreateClass_Click(object sender, EventArgs e)
        {
            string cname = txtClassName.Text;
            short cid;
            if (!short.TryParse(txtClassID.Text, out cid))
            {
                RadMessageBoxExtensions.Show("Invalid Class ID", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                return;
            }
            if (Lion.AccountClass.All.ContainsKey(cid))
            {
                RadMessageBoxExtensions.Show("ID already exists", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                return;
            }

            AccountClass accoutClass = new AccountClass() { ID = cid, Name = cname };
            Exception ex;
            Lion.ObjectAssembler.SaveCachedObject<short, AccountClass>(accoutClass, accoutClass.ID, accoutClass, out ex);

            if (null != ex)
            {
                RadMessageBoxExtensions.Show("Error While Saving Account Class", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);

            }
            else
            {
                RadMessageBoxExtensions.Show("Saved Successfuly");
                tbAccountClass_Click(null, null);
            }

        }

        private void gvAllAccounts_SelectionChanged(object sender, EventArgs e)
        {
            if (cbxAccountUpdate.Checked)
            {
                Lion.Account selectedAccount = gvAllAccounts.SelectedRows.First().DataBoundItem as Lion.Account;
                if (null != selectedAccount)
                {
                    ddlAccountTypes.SelectedItem = ddlAccountTypes.FindItem(selectedAccount.AccountCategory.ToString());
                    txtAccountName.Text = selectedAccount.Name;
                    var ID = selectedAccount.ID.Split('-');
                    txtAccountIDf.Text = ID[0];
                    txtAccountIDs.Text = ID[1];

                    this.AccountUpdated = selectedAccount;
                }

            }
        }

        private void cbxAccountUpdate_Click(object sender, EventArgs e)
        {
            btnUpdateAccount.Visible = true;
            lblUpdateMsg.Visible = true;
            btnCreateAccount.Visible = false;
            txtAccountIDs.Enabled = false;

        }

        private void btnUpdateAccount_Click(object sender, EventArgs e)
        {
            AccountCategory accountCategory = (AccountCategory)ddlAccountTypes.SelectedValue;

            string name = txtAccountName.Text;
            if (null == AccountUpdated)
            {
                return;
            }
            
            AccountUpdated.Name = name;
            AccountUpdated.AccountCategory = accountCategory;


            Exception ex;

            ObjectAssembler.SaveOrUpdateCachedObject<string, Account>(AccountUpdated, AccountUpdated.ID, AccountUpdated, out ex);

            if (null != ex)
            {
                RadMessageBoxExtensions.Show("Error While Updating Account", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);

            }
            else
            {
                RadMessageBoxExtensions.Show("Updated Successfuly");
                txtAccountName.Text = string.Empty;
                txtAccountIDs.Text = string.Empty;
                txtAccountIDf.Text = string.Empty;
                cbxAccountUpdate.Checked = false;
                btnUpdateAccount.Visible = false;
                btnCreateAccount.Visible = true;
                lblUpdateMsg.Visible = false;
                txtAccountIDs.Enabled = true;
                tabItemViewAllAccounts_Click(null, null);
            }



        }






    }
}
